package cn.com.lsjz.nursemanager.service.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.com.lsjz.commons.utils.UpdateTool;
import cn.com.lsjz.nursemanager.model.system.SysDept;
import cn.com.lsjz.nursemanager.repository.system.SysDeptRepository;

@Service
public class SysDeptServiceImpl implements SysDeptService {
	
	@Autowired
	private SysDeptRepository deptDictRepository;
	
	@Override
	public List<SysDept> getDeptDictByUserId(String userId) {
		return deptDictRepository.loadByUserId(userId);
	}

	@Override
	public List<SysDept> getDeptList(String deptName) {
		if(StringUtils.isEmpty(deptName)) {
			return deptDictRepository.findAll();
		}else {
			return deptDictRepository.getByDeptNameContaining(deptName);
		}
	}

	@Override
	public void activeDept(Integer id, Integer active) {
		deptDictRepository.updateByHql("update DeptDict d set d.active = ?1 where d.id = ?2"
								, active
								, id);
	}

	@Override
	public SysDept getDeptById(Integer id) {
		return deptDictRepository.findOne(id);
	}

	@Override
	public SysDept saveDept(SysDept deptDict) {
		if(deptDict.getActive() == null) // 默认设置成启用状态
			deptDict.setActive(1);
		return deptDictRepository.save(deptDict);
	}

	@Override
	public SysDept editDept(SysDept deptDict) {
		SysDept dept = deptDictRepository.findOne(deptDict.getId());
		UpdateTool.copyNullProperties(dept, deptDict);
		return deptDictRepository.save(deptDict);
	}

	@Override
	public void delDept(Integer id) {
		deptDictRepository.delete(id);
	}

}
